package com.example.lifecycledemo;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

/**
 * Created by xiuzhizhu
 * Date: 2020/12/24
 */

// 若是 Activity，则需要手动实现 LifecycleOwner 接口，将实例化好的 LifecycleRegistry 类返回
public class MainActivity2 extends Activity implements LifecycleOwner {

    private final LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyLocationListener myLocationListener = new MyLocationListener(this,
                new MyLocationListener.OnLocationChangedListener() {
                    @Override
                    public void onChange(double latitude, double longitude) {
                        // 位置发生变化时，调用该方法
                    }
                });

        getLifecycle().addObserver(myLocationListener);
    }

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }


}
